package com.myk.game.gobangchess.rooms.oldrooms;

import com.myk.game.gobangchess.CustomBottom;
import com.myk.game.gobangchess.DataManager;
import com.myk.game.gobangchess.constants.WinMode;
import com.myk.game.gobangchess.signs.*;
import com.myk.game.gobangchess.constants.BoardStyleData;
import com.myk.game.gobangchess.utils.GridsUtils;
import com.myk.game.gobangchess.Room;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//捡石子棋房间
public class StonePickRoom extends Room {

    boolean rule1selected, rule2selected;
    boolean rule1,rule2;

    boolean startSelected;
    int startX, startY;

    CustomBottom R1On = new CustomBottom(this, "启用一", (player) -> {
        rule1 = true;
        rule1selected = true;
        noticeAllBroadcast("规则一启用了");
        tryDoRealStart();
    }), R1Off = new CustomBottom(this, "不启用一", (player) -> {
        rule1 = false;
        rule1selected = true;
        noticeAllBroadcast("规则一不启用了");
        tryDoRealStart();
    }), R2On = new CustomBottom(this, "启用二", (player) -> {
        rule2 = true;
        rule2selected = true;
        noticeAllBroadcast("规则二启用了");
        tryDoRealStart();
    }), R2Off = new CustomBottom(this, "不启用二", (player) -> {
        rule2 = false;
        rule2selected = true;
        noticeAllBroadcast("规则二不启用了");
        tryDoRealStart();
    }), Cancal = new CustomBottom(this, "取消", (player) -> {
        startSelected = false;
        player.clearCustomBottom();
        player.dispatchCustomBottom();
        signList.clear();
        noticeAllRefreshGameInfo();
    });

    private void tryDoRealStart() {
        if (rule1selected && rule2selected) {
            DataManager.noticeAllGameStart(player_black, player_white, roomName);
            player_black.clearCustomBottom();
            player_white.clearCustomBottom();
            player_black.dispatchCustomBottom();
            player_white.dispatchCustomBottom();
            startSelected = false;
            gameTip = "请" + player_black.getNickNameOrTemp() + "选择串起点";
            Random r = new Random();
            for (int i = r.nextInt(8) + 48; i > 0; ) {
                int x = r.nextInt(8), y = r.nextInt(8);
                if (curGrids[x][y] == 0) {
                    curGrids[x][y] = r.nextInt(rule1 ? 2 : 1) + 1;
                    i--;
                }
            }
            noticeBlackMove();
            preHistory();
        }
    }

    public StonePickRoom(String roomName) {
        super(roomName, "\t\t夏日设计，逆界群友帮助完善，增加了许多特殊规则的捡石子游戏。", "逆界捡石子", BoardStyleData.getGoBangStyle(8, 8), true, true);
    }

    @Override
    public String getRuleText() {
        return "(测试状态：不记录对弈数据)\n" +
                "8x8棋盘，开局随机放48-56枚棋子\n" +
                "双方轮流行动，每次行动可以把一串同色棋子拿走\n" +
                "每捡拿走最后一枚棋子的输\n" +
                "可选规则一：棋子可能有黑白双色，允许拿一串黑白数量相同的棋" + (rule1selected ? rule1 ? "（已选）" : "（未选）" : "") + "\n" +
                "可选规则二：使规则改为拿走最后一枚棋子的赢" + (rule2selected ? rule2 ? "（已选）" : "（未选）" : "");
    }

    @Override
    public void resetGrids() {
        step = 0;
        gameTip = "-1";
        overFlag = 0;
        signList.clear();
        rule1 = false;
        rule2 = false;
        rule1selected = false;
        rule2selected = false;
        curGrids = GridsUtils.copyGrids(originGrids);
        startSelected = false;
    }

    @Override
    protected void doStart() {
        DataManager.noticeAllGameStart(player_black, player_white, roomName);
        overFlag = 1;//重置结束状态：对弈进行中未结束
        playingStartTime = System.currentTimeMillis(); //重置对弈开始时间
        resetGrids();

        if (player_black == player_white) { //单人模式,默认规则1是选中的，询问是否选中规则2
            player_black.clearCustomBottom();
            player_black.registerCustomBottom(R1On);
            player_black.registerCustomBottom(R1Off);

            player_black.registerCustomBottom(R2On);
            player_black.registerCustomBottom(R2Off);
            player_black.dispatchCustomBottom();
            gameTip = "是否启用规则一和规则二？";
        }
        else {
            if (player_black != null) {
                player_black.clearCustomBottom();
                if (!rule1selected) {
                    player_black.registerCustomBottom(R1On);
                    player_black.registerCustomBottom(R1Off);
                }
                player_black.dispatchCustomBottom();
            }
            if (player_white != null) {
                player_white.clearCustomBottom();
                if (!rule2selected) {
                    player_white.registerCustomBottom(R2On);
                    player_white.registerCustomBottom(R2Off);
                }
                player_white.dispatchCustomBottom();
            }
            gameTip = "请黑方决定是否启用规则一，白方决定是否启用规则二？";
        }

        noticeNoMove();
    }

    @Override
    public void doRule(int row, int col, int value) {
        if (curGrids[row][col] != 0) {
            List<Integer> signIds = new ArrayList<>();
            if (startSelected) {
                if (startX == row) {
                    int y1, y2;
                    if (startY > col) {
                        y1 = col;
                        y2 = startY;
                    }
                    else {
                        y1 = startY;
                        y2 = col;
                    }
                    int[] cnt = new int[]{0, 0, 0};
                    for (int i = y1; i <= y2; i++) {
                        cnt[curGrids[row][i]]++;
                    }
                    if (cnt[0] == 0 && ((cnt[1] == 0 || cnt[2] == 0) || (rule1 && cnt[1] == cnt[2]))) {
                        for (int i = y1; i <= y2; i++) {
                            curGrids[row][i] = 0;
                            signIds.add(getIndex(row, i));
                        }
                        endRule(value, signIds);
                    }
                }
                else if (startY == col) {
                    int x1, x2;
                    if (startX > row) {
                        x1 = row;
                        x2 = startX;
                    }
                    else {
                        x1 = startX;
                        x2 = row;
                    }
                    int[] cnt = new int[]{0, 0, 0};
                    for (int i = x1; i <= x2; i++) {
                        cnt[curGrids[i][col]]++;
                    }
                    if (cnt[0] == 0 && ((cnt[1] == 0 || cnt[2] == 0) || (rule1 && cnt[1] == cnt[2]))) {
                        for (int i = x1; i <= x2; i++) {
                            curGrids[i][col] = 0;
                            signIds.add(getIndex(i, col));
                        }
                        endRule(value, signIds);
                    }
                }
            }
            else {
                if (curGrids[row][col] != 0) {
                    startSelected = true;
                    startX = row;
                    startY = col;
                    signList.clear();
                    signList.add(
                            new AroundSign(new ArrayList<Integer>() {
                                {
                                    add(getIndex(row, col));
                                }
                            },
                                    "#66FFDD33", "#FFFFDD00", 1.2f));
                    if (value == 1) {
                        gameTip = "请" + player_black.getNickNameOrTemp() + "选择串终点";
                        player_black.registerCustomBottom(Cancal);
                        player_black.dispatchCustomBottom();
                        noticeBlackMove();
                    }
                    else {
                        gameTip = "请" + player_white.getNickNameOrTemp() + "选择串终点";
                        player_white.registerCustomBottom(Cancal);
                        player_white.dispatchCustomBottom();
                        noticeWhiteMove();
                    }
                }
            }
        }
    }

    private void endRule(int value, List<Integer> signIds) {
        stepAdd();
        startSelected = false;
        boolean empty = true;
        signList.clear();
        signList.add(new AroundSign(signIds, "#66FFDD33", "#FFFFDD00", 1.2f));
        for (int x = 0; x < 8; x++) {
            for (int y = 0; y < 8; y++) {
                if (curGrids[x][y] != 0) {
                    empty = false;
                    break;
                }
            }
        }
        if (value == 1) {
            player_black.clearCustomBottom();
            player_black.dispatchCustomBottom();
        }
        else {
            player_white.clearCustomBottom();
            player_white.dispatchCustomBottom();
        }
        if (empty) {
            value = rule2 ? 3 - value : value;
            if (value == 1) {
                doOver(WinMode.WHITE_WIN, player_white.getNickNameOrTemp() + "战胜" + player_black.getNickNameOrTemp());
            }
            else {
                doOver(WinMode.BLACK_WIN, player_black.getNickNameOrTemp() + "战胜" + player_white.getNickNameOrTemp());
            }
        }
        else {
            if (value == 1) {
                gameTip = "请" + player_white.getNickNameOrTemp() + "选择串起点";
                noticeWhiteMove();
            }
            else {
                gameTip = "请" + player_black.getNickNameOrTemp() + "选择串起点";
                noticeBlackMove();
            }
        }
    }
}